package com.sjhy.plugin.ui;

import com.fasterxml.jackson.core.type.TypeReference;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.ui.InputValidator;
import com.intellij.openapi.ui.Messages;
import com.intellij.util.ObjectUtils;
import com.sjhy.plugin.api.EasyCodeBambooApi;
import com.sjhy.plugin.api.TypeMapperGroupApi;
import com.sjhy.plugin.constants.ConfigValue;
import com.sjhy.plugin.constants.MsgValue;
import com.sjhy.plugin.dto.TypeMapper;
import com.sjhy.plugin.dto.TypeMapperGroup;
import com.sjhy.plugin.dto.TypeMapperGroupListDto;

import com.sjhy.plugin.entity.TypeMapperModel;
import com.sjhy.plugin.tool.CloneUtils;
import com.sjhy.plugin.config.Settings;
import com.sjhy.plugin.tool.MessageDialogUtils;
import com.sjhy.plugin.tool.StringUtils;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.Map;

/**
 * 类型映射设置
 *
 * @author makejava
 * @version 1.0.0
 * @since 2018/07/17 13:10
 */
public class TypeMapperSetting implements Configurable {
    /**
     * 主面板
     */
    private JPanel mainPanel;
    /**
     * 类型映射分组切换下拉框
     */
    private JComboBox<TypeMapperGroup> typeMapperComboBox;
    /**
     * 分组新增按钮
     */
    private JButton typeMapperAddButton;
    /**
     * 类型映射表
     */
    private JTable typeMapperTable;
    /**
     * 添加映射按钮
     */
    private JButton addButton;
    /**
     * 移除映射按钮
     */
    private JButton removeButton;
    /**
     * 删除分组按钮
     */
    private JButton deleteButton;
    /**
     * 操作结果
     */
    private JLabel resultLabel;

    /**
     * 是否初始化完成
     */
    private boolean init;
    /**
     * 类型映射表模型
     */
    private TypeMapperModel typeMapperModel;


    private TypeMapperGroupListDto typeMapper;


    public TypeMapperSetting(TypeMapperGroupListDto typeMapper) {

        this.typeMapper = typeMapper;

        //添加类型
        addButton.addActionListener(e -> {
            if (!init) {
                return;
            }
            final TypeMapperGroup selectedItem = (TypeMapperGroup) typeMapperComboBox.getSelectedItem();
            if (selectedItem == null) {
                return;
            }
            TypeMapperGroupApi.addTypeMapper(selectedItem.getId());
            this.typeMapper = EasyCodeBambooApi.getCurrentProject("");
            refresh();
        });
        //移除类型
        removeButton.addActionListener(e -> {

            int[] selectRows = typeMapperTable.getSelectedRows();
            if (selectRows == null || selectRows.length == 0) {
                return;
            }
            if (!MessageDialogUtils.yesNo("确认删除?")) {
                return;
            }
            // 从后面往前面移除，防止下标错位问题。
            for (int i = selectRows.length - 1; i >= 0; i--) {
                typeMapperModel.removeRow(selectRows[i]);
            }
        });

        //切换分组
        typeMapperComboBox.addActionListener(e -> {
            if (!init) {
                return;
            }
            final TypeMapperGroup selectedItem = (TypeMapperGroup) typeMapperComboBox.getSelectedItem();
            if (selectedItem == null) {
                return;
            }
            final Integer id = selectedItem.getId();
            if (id.equals(this.typeMapper.getCurrentTypeMapperGroup().getId())) {
                return;
            }
            this.typeMapper = EasyCodeBambooApi.getCurrentProject(id.toString());
            refresh();
        });

        //新增分组按钮
        typeMapperAddButton.addActionListener(e -> {
            String value = Messages.showInputDialog("分组名称:", "输入分组名称:", Messages.getQuestionIcon(), " 新增", new InputValidator() {
                @Override
                public boolean checkInput(String inputString) {
                    return true;
                }

                @Override
                public boolean canClose(String inputString) {
                    return this.checkInput(inputString);
                }
            });
            if (value == null) {
                return;
            }
            final String result = TypeMapperGroupApi.addTypeMapperGroup(value);
            resultLabel.setText(result);
            this.typeMapper = EasyCodeBambooApi.getCurrentProject("");
            refresh();
        });

        //删除分组
        deleteButton.addActionListener(e -> {
            final TypeMapperGroup selectedItem = (TypeMapperGroup) typeMapperComboBox.getSelectedItem();
            if (MessageDialogUtils.yesNo("确认删除分组 " + selectedItem.getGroupName() + "?")) {
                if (ConfigValue.DEFAULT_NAME.equals(selectedItem.getGroupName())) {
                    Messages.showWarningDialog("无法删除默认分组!", MsgValue.TITLE_INFO);
                    return;
                }
                final String result = TypeMapperGroupApi.removeTypeMapperGroup(selectedItem.getId());
                resultLabel.setText(result);
                this.typeMapper = EasyCodeBambooApi.getCurrentProject("");
                refresh();
            }
        });

        // 初始化操作
        init();
    }


    /**
     * 初始化方法
     */
    private void init() {
        //初始化表格
        this.typeMapperModel = new TypeMapperModel();
        this.typeMapperTable.setModel(typeMapperModel);
        refresh();
    }

    /**
     * 刷新方法
     */
    private void refresh() {
        init = false;
        //初始化下拉框
        this.typeMapperComboBox.removeAllItems();
        this.typeMapper.getTypeMapperGroups().forEach(e -> this.typeMapperComboBox.addItem(e));
        this.typeMapperComboBox.setSelectedItem(this.typeMapper.getCurrentTypeMapperGroup());
        this.typeMapperModel.init(this.typeMapper.getTypeMappers());
        init = true;

    }

    @Nls
    @Override
    public String getDisplayName() {
        return "类型映射";
    }

    @Nullable
    @Override
    public String getHelpTopic() {
        return null;
    }

    @Nullable
    @Override
    public JComponent createComponent() {
        return mainPanel;
    }

    @Override
    public boolean isModified() {
        return false;
    }

    @Override
    public void apply() {
    }

    @Override
    public void reset() {
//        this.typeMapperGroupMap = CloneUtils.cloneByJson(settings.getTypeMapperGroupMap(), new TypeReference<Map<String, TypeMapperGroup>>() {
//        });
//        this.currGroupName = settings.getCurrTypeMapperGroupName();
        init();
    }

}
